package com.ym.b2b.type.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.type.dao.EmBNewsTypeDAO;
import com.ym.b2b.type.service.EmBNewsTypeService;
import com.ym.b2b.type.sqlbean.EmBNewsType;
import com.zc.commons.base.Result;

/***
 * @ClassName:
 * @Description:
 * @Author:chinavane
 * @Date: 2015年1月7日 下午2:20:47
 * @Modify
 * @CopyRight 珍诚医药在线股份有限公司
 */
@Service("emBNewsTypeService")
public class EmBNewsTypeServiceImpl extends BaseService implements EmBNewsTypeService {

    @Autowired
    private EmBNewsTypeDAO newsTypeDao;

    @Override
    public EmBNewsType searchEmBNewsType(EmBNewsType emBNewsType) {
        EmBNewsType result = new EmBNewsType();
        result = newsTypeDao.searchEmBNewsType(emBNewsType.getNewsTypeId());
        return result;
    }

    @Override
    public List searchEmBNewsTypeByParentId(EmBNewsType emBNewsType) {
        // TODO Auto-generated method stub
        List<EmBNewsType> list = new ArrayList<EmBNewsType>();
        list = newsTypeDao.searchEmBNewsTypeByParentId(emBNewsType);

        for (int i = 0; i < list.size(); i++) {
            list.get(i).setId(list.get(i).getNewsTypeId());
            list.get(i).set_parentId(list.get(i).getParentid());
            EmBNewsType getChild = new EmBNewsType();

            getChild.setParentid(list.get(i).getNewsTypeId());

            long hasChild = newsTypeDao.searchEmBNewsTypeNum(getChild);

            if (hasChild > 0) {
                list.get(i).setState("closed");
            }
        }

        return list;
    }

    /**
     * 设置缓存key
     */
    @Cacheable(value = "category", key = "#emBNewsType.cacheKey", condition = "#emBNewsType.searchFlag eq 'tag'")
    @Override
    public List<EmBNewsType> searchEmBNewsTypeByParentIdToComboTree(EmBNewsType emBNewsType) {

        List<EmBNewsType> list = new ArrayList<EmBNewsType>();
        emBNewsType.setParentid((long) 0);
        list = newsTypeDao.searchEmBNewsTypeByParentId(emBNewsType);
        RecursiveChild(list);
        return list;
    }

    /**
     * @Title: RecursiveChild
     * @Description: 递归子类以便适应easyui comboTree的json数据格式
     * @Param @param emBNewsType
     * @Param @return
     * @Return List<EmBNewsType>
     * @Throws
     */
    public List<EmBNewsType> RecursiveChild(List<EmBNewsType> list) {

        for (int i = 0; i < list.size(); i++) {
            list.get(i).setId(list.get(i).getNewsTypeId());
            list.get(i).setText(list.get(i).getName());

            // 获取子节点
            EmBNewsType getChild = new EmBNewsType();

            List<EmBNewsType> childList = new ArrayList<EmBNewsType>();

            getChild.setParentid(list.get(i).getNewsTypeId());

            childList = newsTypeDao.searchEmBNewsTypeByParentId(getChild);

            if (childList.size() > 0) {
                list.get(i).setChildren(childList);

                RecursiveChild(childList);

            }
        }
        return list;
    }

    /**
     * @Title: RecursiveChild
     * @Description: 递归子类以便获取所有子节点的ID值
     * @Param @param emBNewsType
     * @Param @return
     * @Return List
     * @Throws
     */

    List<Long> idList = new ArrayList<Long>();

    public List RecursiveChildID(List<EmBNewsType> list) {

        for (int i = 0; i < list.size(); i++) {
            // idList.add(list.get(i).getNewsTypeId());

            // 获取子节点
            EmBNewsType getChild = new EmBNewsType();

            List<EmBNewsType> childList = new ArrayList<EmBNewsType>();

            getChild.setParentid(list.get(i).getNewsTypeId());

            childList = newsTypeDao.searchEmBNewsTypeByParentId(getChild);

            if (childList.size() > 0) {
                for (int j = 0; j < childList.size(); j++) {
                    idList.add(childList.get(j).getNewsTypeId());
                }

                RecursiveChildID(childList);

            }
        }
        return idList;
    }

    @Override
    public Result insertNewsType(EmBNewsType emBNewsType) {
        Result result = new Result();
        try {
            newsTypeDao.insertEmBNewsType(emBNewsType);
            result.setResult("添加信息成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "添加信息失败!");
        }
        return result;
    }

    @Override
    public Result updateEmBNewsType(EmBNewsType emBNewsType) {
        Result result = new Result();
        try {

            if (emBNewsType.getIsIndex() == null) {
                emBNewsType.setIsIndex(0);
            }

            if (emBNewsType.getIsShow() == null) {
                emBNewsType.setIsShow(0);
            }

            newsTypeDao.updateEmBNewsType(emBNewsType);
            result.setResult("修改信息成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "修改信息失败!");
        }
        return result;
    }

    @Override
    public void deleteEmBNews(Long newsTypeId) {
        // 通过传递过来的Id值，需要递归遍历所有ID，以便进行子节点的删除工作
        EmBNewsType emBNewsType = new EmBNewsType();
        emBNewsType.setNewsTypeId(newsTypeId);

        List<EmBNewsType> list = new ArrayList<EmBNewsType>();
        list = newsTypeDao.searchEmBNewsTypeByParentId(emBNewsType);
        List idLists = RecursiveChildID(list);

        idLists.add(0, newsTypeId);

        newsTypeDao.deleteEmBNewsType(StringUtils.collectionToCommaDelimitedString(idLists));

    }

    @Override
    public EmBNewsType showCmsTypeDetail(Long newsTypeId) {

        return newsTypeDao.showCmsTypeDetail(newsTypeId);
    }

}
